﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeBox.FB.DataAccess;
using CodeBox.FB.ViewModels;
namespace CodeBox.LINQ.EF.DBFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            TeamDataAccess teamDa = new TeamDataAccess();

            ORTIAEntities db = new ORTIAEntities();
            //var players = (from crew in db.Crews
            //               join player in db.Players on crew.ID equals player.CrewId
            //               join PTS in db.PlayerTeamSeasons on player.ID equals PTS.PlayerId
            //               where PTS.Team.ID == 3 && PTS.Season.ID == 1
            //               group crew by new { crew.ID, crew.Name, crew.Photo } into grp
            //               //select g.Key).ToList<string>();
            //               select new PlayerViewModel { playerID = (int)grp.Key.ID, Name = grp.Key.Name, imageUrl = grp.Key.Photo });

            var score = from pts in db.PlayerTeamSeasons
                        join p in db.Players on pts.PlayerId equals p.ID
                        join c in db.Crews on p.CrewId equals c.ID
                        join f in db.Fixtures on pts.Fixture.Id equals f.Id
                        join t1 in db.Teams on f.TeamID1 equals t1.ID
                        join t2 in db.Teams on f.TeamID2 equals t2.ID
                        where p.CrewId == 27 && pts.Season.ID == 1
                        
                        select new PlayerScoreViewModel
                        {
                            
                         Run1 = (int)(pts.Run1 ?? 0),
                            Run2 = (int)(pts.Run2 ?? 0),
                            Run3 = (int)(pts.Run3 ?? 0),
                            Run4 = (int)(pts.Run4 ?? 0),
                            Run6 = (int)(pts.Run6 ?? 0),
                            BallsBowled = (int)(pts.BallsBowled ?? 0),
                            RunsGiven = (int)(pts.RunsGiven ?? 0),
                            Wickets = (int)(pts.Wickets ?? 0),
                            Catches = (int)(pts.Catches ?? 0),
                            Dot = (int)(pts.Dot ?? 0),
                            NoBall = (int)(pts.NoBall ?? 0),
                            Match = (t1.TeamName + " v/s " + t2.TeamName)
                        };
            foreach (PlayerScoreViewModel playerScore in score)
            {
                Console.WriteLine("Runs " + (playerScore.Run1 + playerScore.Run2 + playerScore.Run3 + playerScore.Run4 + playerScore.Run6) + " Match:" + playerScore.Match);

            }
            Console.ReadKey();
        }
    }
}
